package com.hcy.schedulingapi.controller;

import com.hcy.rabbitbasicutils.utils.RedisUtils;
import com.hcy.schedulingclient.model.driver.request.DispatcherRequestDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/1/13 18:37
 */
@RestController
@RequestMapping("/search")
public class SearchLocationController {

    Logger logger = LoggerFactory.getLogger(SearchLocationController.class);

    /**
     * redis geo key
     */
    @Value("${CAR_GEO_KEY}")
    private String CAR_GEO_KEY;

    @Autowired
    RedisUtils redisUtils;

    /**
     * @Author huchenying
     * @Description 筛选符合的司机
     * @Date 2022/1/13 19:01
     **/
    @PostMapping("/dispatcher")
    public ResponseData<List<Integer>> dispatcher(@Validated @RequestBody DispatcherRequestDTO requestDTO) {
        logger.info("dispatcher start param - > {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<List<Integer>> responseData = new ResponseData<>();
        try {
            List<Integer> result = redisUtils.searchGeo(CAR_GEO_KEY, requestDTO.getLat(), requestDTO.getLog(), requestDTO.getDistance());
            responseData.setCode(Code.Success);
            responseData.setData(result);
            return responseData;
        } catch (Exception e) {
            logger.error("查询失败！", e);
            responseData.setCode(Code.Fail);
            responseData.setMsg("查询附近车辆失败！");
            return responseData;
        }
    }

}
